import { memo, useEffect, useState, useRef, lazy, Suspense } from 'react'
import { Button } from 'antd'
import LdPrintTable from '../../../../components/LdPrintTable'
import bus from '../../../../utils/eventBus'
const DialogView = lazy(() => import('./DetailView'))
const SecretLevel = () => {
    const [tableData, setTableData] = useState<any[]>([])
    const columns = useRef([
        {
            title: '人员等级',
            dataIndex: 'authSecretName',
            key: 'authSecretName'
        },
        {
            title: '授权密级',
            key: 'data',
            render: (_: any, record: any) => {
                return (
                    <>
                        {record.data.map((item: any, index: number) => item.secretLabel).join(';')}
                    </>
                )
            }
        },
        {
            title: '操作',
            key: 'action',
            width: 80,
            render: (_: any, record: any) => {
                return (
                    <>
                        <Button style={{ padding: 0 }} type="link" onClick={() => handleViewDetail(record)}>编辑</Button>
                    </>
                )
            }
        },
    ])
    const [dialog, setDialog] = useState<{ visible: boolean, entity: any }>({
        visible: false,
        entity: undefined
    })
    const getUserAuthSecretConfigFinished = (res: any) => {
        if (res.code === 200) {
            res.data.map((item: any, index: number) => {
                item.key = index
            })
            setTableData(res.data)
        }
    }
    const loadData = () => {
        window.chromePro.getUserAuthSecretConfig();
    }
    const handleViewDetail = (row: any) => {
        setDialog({
            visible: true,
            entity: { ...row }
        })
    }
    const close = () => {
        setDialog({
            ...dialog,
            visible: false,
            entity: null
        })
    }
    useEffect(() => {
        loadData()
        bus.addListener('getUserAuthSecretConfigFinished', getUserAuthSecretConfigFinished)
    }, [])
    useEffect(() => {
        return () => {
            bus.removeListener('getUserAuthSecretConfigFinished', getUserAuthSecretConfigFinished)
        }
    }, [])
    return (
        <>
            <LdPrintTable rowSelection={false} showPage={false} data={tableData} columns={columns.current}></LdPrintTable>
            <Suspense>
                {
                    dialog.visible ? <DialogView dialog={dialog} close={close} refresh={loadData} /> : null
                }
            </Suspense>
        </>
    )
}

export default memo(SecretLevel)